/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.business.objects;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cred.industries.core.exceptions.ApplicationException;
import com.cred.industries.core.server.ServerConfig;
import com.cred.industries.core.server.ServerConfigEnum;
import com.cred.industries.core.transferObj.result.ReturnCodes;
import com.cred.industries.core.util.SessionKey;

public class ForgotPasswordBO {
	
	private final static Logger logger = LoggerFactory.getLogger(ForgotPasswordBO.class);

	String mTempPasswordID;
	int mCustomerId = -1;
	String mNewPassword;
	boolean mIsValid;
	DateTime mTtimeIssued;
	
	public ForgotPasswordBO(int customerId) {
	
		mCustomerId = customerId;
		
		mTempPasswordID = SessionKey.createSessionKey(16);
		mNewPassword = SessionKey.createSessionKey(16);
		mIsValid = true;
		mTtimeIssued = new DateTime(DateTimeZone.UTC);
	}
	
	public ForgotPasswordBO(String tempPasswordID, int customerId, String newPassword, boolean isValid, DateTime timeIssued) {
		
		mTempPasswordID = tempPasswordID;
		mCustomerId = customerId;
		mNewPassword = newPassword;
		mIsValid = isValid;
		mTtimeIssued = timeIssued;
	}

	public String getTempPasswordID() {
		return mTempPasswordID;
	}

	public int getCustomerId() {
		return mCustomerId;
	}

	public String getNewPassword() {
		return mNewPassword;
	}

	public void setIsValid(boolean isValid) {
		
		mIsValid = isValid;
	}
	
	public boolean isValid() {
		
		DateTime validAfter = new DateTime(DateTimeZone.UTC);
		int secondsValid = 3600;
		if(ServerConfig.getInstance().hasValue(Integer.class, ServerConfigEnum.FORGOT_PASS_VALID_FOR_SECONDS)) {
			secondsValid = ServerConfig.getInstance().getValue(Integer.class, ServerConfigEnum.FORGOT_PASS_VALID_FOR_SECONDS);
		} else {
			logger.warn("unable to get server config var " + ServerConfigEnum.SESSION_TIMEOUT_SECONDS.toString());
			throw new ApplicationException(ReturnCodes.UNKNOWN_ERROR, "unable to get server config var " + ServerConfigEnum.SESSION_TIMEOUT_SECONDS.toString());
		}

		validAfter = validAfter.minus(secondsValid*1000);
		
		return mIsValid && mTtimeIssued.isAfter(validAfter);
	}

	public DateTime getTimeIssued() {
		return mTtimeIssued;
	}
	
	public int defaultValidForHours() {
		
		int secondsValid = 3600;

		if(ServerConfig.getInstance().hasValue(Integer.class, ServerConfigEnum.FORGOT_PASS_VALID_FOR_SECONDS)) {
			secondsValid = ServerConfig.getInstance().getValue(Integer.class, ServerConfigEnum.FORGOT_PASS_VALID_FOR_SECONDS);
		} else {
			logger.warn("unable to get server config var " + ServerConfigEnum.SESSION_TIMEOUT_SECONDS.toString());
			throw new ApplicationException(ReturnCodes.UNKNOWN_ERROR, "unable to get server config var " + ServerConfigEnum.SESSION_TIMEOUT_SECONDS.toString());
		} 
		
		return (int) Math.floor(secondsValid/3600);
	}
}
